﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace AuctionHelper.Core
{
    /// <summary>
    /// 输入设备辅助类
    /// </summary>
    public class InputHelper
    {
        /// <summary>
        /// 鼠标模拟
        /// </summary>
        /// <param name="dwFlags"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <param name="dwData"></param>
        /// <param name="dwExtraInfo"></param>
        [DllImport("user32.dll")]
        protected static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        /// <summary>
        /// 设置光标位置
        /// </summary>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        protected static extern bool SetCursorPos(int X, int Y);

        /// <summary>
        /// 鼠标事件
        /// </summary>
        protected enum MouseEventFlag
        {
            Move        = 0x0001,
            LeftDown    = 0x0002,
            LeftUp      = 0x0004,
            RightDown   = 0x0008,
            RightUp     = 0x0010,
            MiddleDown  = 0x0020,
            MiddleUp    = 0x0040,
            XDown       = 0x0080,
            XUp         = 0x0100,
            Wheel       = 0x0800,
            VirtualDesk = 0x4000,
            Absolute    = 0x8000
        }

        /// <summary>
        /// 键盘事件
        /// </summary>
        protected enum KeyEventFlag
        {
            KeyDown = 0,
            KeyUp = 2
        }

        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void MouseMove(int x, int y)
        {
            SetCursorPos(x, y);
        }

        /// <summary>
        /// 鼠标单击
        /// </summary>
        public static void MouseClick()
        {
            mouse_event((int)MouseEventFlag.LeftDown, 0, 0, 0, 0);
            mouse_event((int)MouseEventFlag.LeftUp, 0, 0, 0, 0);
        }

        /// <summary>
        /// 鼠标双击
        /// </summary>
        public static void MouseDoubleClick()
        {
            MouseClick();
            MouseClick();
        }

        /// <summary>
        /// 鼠标滚轮操作
        /// </summary>
        /// <param name="length"></param>
        public static void MouseWheel(int length)
        {
            mouse_event((int)MouseEventFlag.Wheel, 0, 0, length, 0);
        }

        /// <summary>
        /// 按键操作
        /// </summary>
        /// <param name="key"></param>
        public static void SendKey(string value)
        {
            SendKeys.SendWait(value);
        }
    }
}
